﻿using PIMS.DBModel;
using PIMS.Interface;
using PIMS.Utils;
using PIMS.XmlModel;
using PIMS_Interface.Interface;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace PIMS.WorkPlugin
{
    public partial class FormEdit : Form, IFormEdit
    {
        PIMSConfigX pimsConfig = XmlHelper.XmlFileToObject<PIMSConfigX>(AppDomain.CurrentDomain.BaseDirectory + "config\\PIMSConfig.xml");


        private IDebugMsg debug = null;
        private ILog4net log = null;
        private orderformcurrent order = null;
        private int lineNO;
        private bool bresult;
        public orderformcurrent RetValue
        {
            get
            {
                return this.order;
            }
            set
            {
                this.order = value;
            }
        }

        public FormEdit()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 注：反射加载时会自动调用一次
        /// </summary>
        public void Init(IDebugMsg debugMsg, ILog4net log4net)
        {
            this.debug = debugMsg;
            this.log = log4net;

            //try
            //{
            //    InitComboBoxByString(this.cb包装设备, pimsConfig.GetAppValueByKey("deviceName"));
            //    DataDictTools.InitComboBoxByDataDic(this.cb品名, "品名");
            //    DataDictTools.InitComboBoxByDataDic(this.cb牌号, "牌号");
            //    DataDictTools.InitComboBoxByDataDic(this.cb等级, "等级");
            //}
            //catch (Exception ex)
            //{
            //    this.debug.DebugMsg(ex.Message, InfoLevel.error);
            //    this.log.Error("", ex);
            //}
        }

        private void FormEdit_Load(object sender, EventArgs e)
        {
            Dictionary<int, string> kvDictonary1 = new Dictionary<int, string>();
            kvDictonary1.Add(1, "西线");
            kvDictonary1.Add(2, "东线");

            BindingSource bs1 = new BindingSource();
            bs1.DataSource = kvDictonary1;
            cbxLineNo.DataSource = bs1;
            cbxLineNo.ValueMember = "Key";
            cbxLineNo.DisplayMember = "Value";


            Dictionary<int, string> kvDictonary = new Dictionary<int, string>();
            kvDictonary.Add(1, "木托");
            kvDictonary.Add(2, "塑托");
            BindingSource bs = new BindingSource();
            bs.DataSource = kvDictonary;
            cbxPack.DataSource = bs;
            cbxPack.ValueMember = "Key";
            cbxPack.DisplayMember = "Value";


            Dictionary<int, string> kvDictonary2 = new Dictionary<int, string>();
            kvDictonary2.Add(1, "待检");
            kvDictonary2.Add(2, "合格品");
            kvDictonary2.Add(3, "客户合格品");
            kvDictonary2.Add(4, "优等品");
            kvDictonary2.Add(99, "不合格");
            BindingSource bs2 = new BindingSource();
            bs2.DataSource = kvDictonary2;
            cbxState.DataSource = bs2;
            cbxState.ValueMember = "Key";
            cbxState.DisplayMember = "Value";

            Dictionary<int, string> kvDictonary3 = new Dictionary<int, string>();
            kvDictonary3.Add(1, "生产入库");
            kvDictonary3.Add(2, "退货入库");
            BindingSource bs3 = new BindingSource();
            bs3.DataSource = kvDictonary3;
            cbxType.DataSource = bs3;
            cbxType.ValueMember = "Key";
            cbxType.DisplayMember = "Value";
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.err.Clear();

            if (String.IsNullOrEmpty(this.cbxLineNo.Text))
            {
                this.err.SetError(this.cbxLineNo, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtOrderNum.Text))
            {
                this.err.SetError(this.txtOrderNum, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtGrade.Text))
            {
                this.err.SetError(this.txtGrade, "禁止为空！");
                return;
            }
            //if (String.IsNullOrEmpty(this.txtMaterialNo.Text))
            //{
            //    this.err.SetError(this.txtMaterialNo, "禁止为空！");
            //    return;
            //}
            //if (String.IsNullOrEmpty(this.txtMaterialName.Text))
            //{
            //    this.err.SetError(this.txtMaterialName, "禁止为空！");
            //    return;
            //}
            if (String.IsNullOrEmpty(this.txtBatch.Text))
            {
                this.err.SetError(this.txtBatch, "禁止为空！");
                return;
            }
            //if (String.IsNullOrEmpty(this.txtWeight.Text))
            //{
            //    this.err.SetError(this.txtWeight, "禁止为空！");
            //    return;
            //}
            //if (!decimal.TryParse(this.txtWeight.Text,out decimal dd))
            //{
            //    this.err.SetError(this.txtWeight, "必须为数字！");
            //    return;
            //}
            #region 注释掉

            //if (this.txtProductLine.Text.Length != 7)
            //{
            //    this.err.SetError(this.txtProductLine, "字符串长度异常！");
            //    return;
            //}
            //if (!IsInt(this.txtProductLine.Text.Substring(5)))
            //{
            //    this.err.SetError(this.txtProductLine, "格式异常！");
            //    return;
            //}
            //if (String.IsNullOrEmpty(this.cbxPalRule.Text))
            //{
            //    this.err.SetError(this.cbxPalRule, "禁止为空！");
            //    return;
            //}
            //if (!IsDouble(this.cbxPalRule.Text))
            //{
            //    this.err.SetError(this.cbxPalRule, "请输入正浮点数！");
            //    return;
            //}

            //lineNO = int.Parse(this.txtProductLine.Text.Substring(5));
            //if (lineNO > 4)
            //{
            //    this.err.SetError(this.txtProductLine, "格式异常！");
            //    return;
            //}
            //string dt = DateTime.Now.ToString("yyyy-MM-dd");
            //string dtTemp = DateTime.Now.ToString("yy-MM-dd");
            //string[] dateItem = dtTemp.Split('-');
            //string batch = dateItem[0] + monthDict[dateItem[1]] + dateItem[2] + this.txtProductLine.Text;
            //string smallBatch = dateItem[2] + "01";
            #endregion

            orderformcurrent c = new orderformcurrent();
            //TODO 1030
            c.packager = ((KeyValuePair<int, string>)cbxLineNo.SelectedItem).Value.ToString();    //产线
            c.workOrder = this.txtOrderNum.Text; //订单号
            c.grade = this.txtGrade.Text;           //牌号

            c.productName = this.txtMaterialName.Text;  //物料名称
            c.productMark = this.txtMaterialNo.Text;    //物料编码
            c.batchNo = this.txtBatch.Text;     //批次
           
            //c.reserved1 = this.txtWeight.Text; //重量
            c.reserved2 = ((KeyValuePair<int, string>)cbxPack.SelectedItem).Value.ToString();  //包装  "restack":1   （1常规-木托 2 翻垛-塑托）
            c.reserved3 = ((KeyValuePair<int, string>)cbxState.SelectedItem).Value.ToString(); //productGrade ，页面叫 业务状态，默认待检
            c.reserved4 = ((KeyValuePair<int, string>)cbxType.SelectedItem).Value.ToString();  //productType 业务类型： 1、生产入库  2、退货入库
            //c.reserved5 = 是否尾托盘 isLast （0 否 1 是），相当于结批的一个标识。
            //c.reserved2 = DateTime.Now.ToString("yyyy");

            //TODO 1101


            RetValue = c;
            this.Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.order = null;
            this.Close();
        }

        private void InitComboBoxByString(ComboBox comboBox, string value)
        {
            comboBox.Items.Clear();
            string[] strs = value.Split('|');
            for (int i = 0; i < strs.Length; i++)
            {
                comboBox.Items.Add(strs[i]);
            }
        }

        /// <summary>
        /// 正则表达式验证中文字符
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool JustChinese(string text = "")
        {
            for (int i = 0; i < text.Length; i++)
            {
                //判断汉字的Unicode编码范围
                if (Regex.IsMatch(text[i].ToString(), @"[\u4e00-\u9fbb]"))
                    return true;
            }
            return false;
        }

        //判断是否为正整数
        public static bool IsInt(string inString)
        {
            Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
            return regex.IsMatch(inString.Trim());
        }

        //判断字符串是否可以转化为正浮点数
        public static bool IsDouble(string inString)
        {
            Regex regex = new Regex("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
            return regex.IsMatch(inString.Trim());
        }
    }
}
